<template>
  <div class="textbook-category">
    <el-card class="page-card">
      <template #header>
        <div class="card-header">
          <span>教材分类</span>
          <el-button type="primary" @click="handleCreate">新增分类</el-button>
        </div>
      </template>

      <el-table :data="tableData" v-loading="loading" row-key="id">
        <el-table-column label="分类名称" prop="name" />
        <el-table-column label="排序" prop="sort" width="100" />
        <el-table-column label="描述" prop="description" show-overflow-tooltip />
        <el-table-column label="创建时间" prop="createTime" width="180" />
        <el-table-column label="操作" fixed="right" width="200">
          <template #default="{ row }">
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分类编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="formRules"
        label-width="80px"
      >
        <el-form-item label="分类名称" prop="name">
          <el-input v-model="formData.name" placeholder="请输入分类名称" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number v-model="formData.sort" :min="0" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input
            v-model="formData.description"
            type="textarea"
            placeholder="请输入分类描述"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref } from 'vue'

const loading = ref(false)
const tableData = ref([])
const dialogVisible = ref(false)
const dialogTitle = ref('')
const currentId = ref('')

const formData = ref({
  name: '',
  sort: 0,
  description: ''
})

const formRules = {
  name: [
    { required: true, message: '请输入分类名称', trigger: 'blur' }
  ]
}

const handleCreate = () => {
  dialogTitle.value = '新增分类'
  dialogVisible.value = true
  currentId.value = ''
  formData.value = {
    name: '',
    sort: 0,
    description: ''
  }
}

const handleEdit = (row) => {
  dialogTitle.value = '编辑分类'
  dialogVisible.value = true
  currentId.value = row.id
  formData.value = { ...row }
}

const handleSubmit = async () => {
  // TODO: 实现提交功能
}

const handleDelete = (row) => {
  // TODO: 实现删除功能
}

// 加载分类列表
const loadCategoryList = async () => {
  // TODO: 实现加载分类列表
}
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
